package springmvcdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springmvcdemo.model.MessageInfo;
import springmvcdemo.service.MessageInfoService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {

    //List<MessageInfo> list = new ArrayList<>();
    @Autowired
    private MessageInfoService messageInfoService;
    //1.保存留言板
    @RequestMapping("/publish")
    public Boolean publish(@RequestBody MessageInfo messageInfo) {//json转对象
        //list.add(messageInfo);
        messageInfoService.insert(messageInfo);
        return true;
    }
    //2.返回留言板
    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageInfoService.selectAll();
    }
    //3.删除一条留言
    @RequestMapping("/delete")
    public String delete(Integer id) {
        //1.查询一下有无id
        if(!messageInfoService.get(id)) return "删除id不存在";
        //2.删除id
        messageInfoService.delete(id);
        return "删除成功";
    }

}
